﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Betop.Lib.Core.Base;
using Betop.Lib.Utilities;
using Betop.Lib.Core.User;
using Betop.Lib.Core.Tree;
using Betop.Lib.Core.Dept;
using Betop.Lib.Enums;


namespace Betop.Lib.Core.Right
{
    public class RecordRightDAL : BaseDAL,IRecordRightDAL
    {
        private string _tableName;

        public RecordRightDAL(string tableName)
        {
            this._tableName = tableName;
        }

        #region IRecordRightDAL 成员

        public string TableName
        {
            set
            {
                _tableName = value;
            }
        }

        public List<int> GetBindIDs(int ownerID, int rightID, int ownerType)
        {
            RightOwnerType type = (RightOwnerType)ownerType;
            string strSql = "select BindID from " + _tableName + " where RightID=" + rightID + " and OwnerID=" + ownerID + " and OwnerType=" + ownerType;
            if (type == RightOwnerType.User)//对于是用户ID的，必须把它所有的部门和所有角色的ID拥有的权限合并起来
            {
                //把创建者加进去
                strSql += " union select BindID from " + _tableName + " where RightID=" + (int)RecordRightName.Creator + " and OwnerID=" + ownerID + " and OwnerType=" + ownerType;

                UserDAL user = new UserDAL();

                List<int> lstDeptIDs = UserDAL.GetDeptIDs(ownerID);
                lstDeptIDs = (new TreeDAL<DeptInfo>("Dept")).GetParentIDs(lstDeptIDs);

                string strDeptIDs = ConvertUtil.ConvertToDbStr(lstDeptIDs);

                strSql += " union select BindID from " + _tableName + " where RightID=" + rightID + " and OwnerID in (" + strDeptIDs + ") and OwnerType=" + (int)RightOwnerType.Dept;

                List<int> lstRoleIDs = user.GetRoleIDs(ownerID);
                string strRoleIDs = ConvertUtil.ConvertToDbStr(lstRoleIDs);
                strSql += " union select BindID from " + _tableName + " where RightID=" + rightID + " and OwnerID in (" + strRoleIDs + ") and OwnerType=" + (int)RightOwnerType.Role;

            }

            DataTable dt = dbOper.Query(strSql);
            List<int> lstBindIDs = ConvertUtil.ConvertToList(dt);
            return lstBindIDs;
        }

        public List<int> GetOwnerIDs(int iBindID, int iRightID, int ownerType)
        {
            string strSql = "select OwnerID from " + _tableName + " where RightID=" + iRightID + " and BindID=" + iBindID + " and OwnerType=" + ownerType;
            DataTable dt = dbOper.Query(strSql);
            List<int> lstOwnerIDs = ConvertUtil.ConvertToList(dt);
            return lstOwnerIDs;
        }

        public void SaveBindIDs(List<int> lstBindIDs, int iOwnerID, int iRightID, int ownerType)
        {
            string[] strSqls = new string[lstBindIDs.Count + 1];
            strSqls[0] = "delete from " + _tableName + " where RightID=" + iRightID + " and OwnerID=" + iOwnerID + " and OwnerType=" + ownerType;
            for (int i = 0; i < lstBindIDs.Count; i++)
            {
                strSqls[i + 1] = "insert into " + _tableName + "(RightID,OwnerID,BindID,OwnerType) values(" + iRightID + "," + iOwnerID + "," + lstBindIDs[i] + "," + ownerType + ")";
            }
            dbOper.RunSql(strSqls);
        }

        public void SaveOwnerIDs(List<int> lstOwnerIDs, int iBindID, int iRightID, int ownerType)
        {
            string[] strSqls = new string[lstOwnerIDs.Count + 1];
            strSqls[0] = "delete from " + _tableName + " where RightID=" + iRightID + " and BindID=" + iBindID + " and OwnerType=" + ownerType;
            for (int i = 0; i < lstOwnerIDs.Count; i++)
            {
                strSqls[i + 1] = "insert into " + _tableName + "(RightID,OwnerID,BindID,OwnerType) values(" + iRightID + "," + lstOwnerIDs[i] + "," + iBindID + "," + ownerType + ")";
            }
            dbOper.RunSql(strSqls);
        }

        public bool HaveRight(int iBindID, int iOwnerID, int iRightID, int ownerType)
        {
            RightOwnerType type = (RightOwnerType)ownerType;
            string strSql = "select count(*) from " + _tableName + " where RightID=" + iRightID + " and OwnerID=" + iOwnerID + " and BindID=" + iBindID + " and OwnerType=" + ownerType;
            if (type == RightOwnerType.User)//对于是用户ID的，必须把它所有的部门和所有角色的ID拥有的权限合并起来
            {
                UserDAL user = new UserDAL();

                List<int> lstDeptIDs = UserDAL.GetDeptIDs(iOwnerID);
                lstDeptIDs = (new TreeDAL<DeptInfo>("Dept")).GetParentIDs(lstDeptIDs);
                string strDeptIDs = ConvertUtil.ConvertToDbStr(lstDeptIDs);


                List<int> lstRoleIDs = user.GetRoleIDs(iOwnerID);
                string strRoleIDs = ConvertUtil.ConvertToDbStr(lstRoleIDs);
                strSql = "select count(*) from " + _tableName + " where ( RightID=" + iRightID + " or RightID = " + (int)RecordRightName.Creator + ") and BindID=" + iBindID + " and "//注意创建者
                                                            + "( OwnerType=" + ownerType + " and OwnerID=" + iOwnerID
                                                            + " or OwnerType=" + (int)RightOwnerType.Dept + " and OwnerID in (" + strDeptIDs + ")"
                                                            + " or OwnerType=" + (int)RightOwnerType.Role + " and OwnerID in (" + strRoleIDs + ")"
                                                            + ")";
            }
            int count = dbOper.GetScalar<int>(strSql);
            bool flag = (count == 0) ? false : true;
            return flag;
        }
        #endregion
    }
}
